package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.mapper.EmployeeMapper;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.SetmeaService;
import com.itheima.reggie.vo.IDdouble;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.HashMap;
import java.util.List;
import java.util.Optional;

@Service
@Slf4j
public class CategoryServiceimpl extends ServiceImpl<CategoryMapper,Category> implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private SetmeaService setmeaService;

    @Autowired
    private DishService dishService;
    /**
     * 分类管理的分类查询
     *
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public IPage<Category> selectPage(Integer page, Integer pageSize) {
        HashMap<String,Integer> map = new HashMap<>();
        map.put("pageNumber",page);
        map.put("PageSize",pageSize);
        IPage<Category> selectPage = new Page<>(map.get("pageNumber"),map.get("PageSize"));
        IPage<Category> page1 = this.page(selectPage);

        return page1;
    }

    /**
     * 新增套餐菜品分类
     */
    @Override
    public boolean savecuisine(Category category) {
       if (category==null){
           return false;
       }
       boolean save = this.save(category);

        return true;
    }
    /**
     * 删除菜品和删除套餐接口
     */
    @Override
    public boolean deletecategory(Long ids) {

        //先查询套餐
        LambdaQueryWrapper<Setmeal> StequeryWrapper = new LambdaQueryWrapper<>();
        StequeryWrapper.eq(ids!=null,Setmeal::getCategoryId,ids);
        Setmeal one = setmeaService.getOne(StequeryWrapper);
        if (one!=null){
            return false;
        }
        //查询菜品
        LambdaQueryWrapper<Dish> Dishqe = new LambdaQueryWrapper<>();
        Dishqe.eq(ids!=null,Dish::getCategoryId,ids);
        int count = dishService.count(Dishqe);
        if (count!=0){
            return false;
        }
        boolean b = this.removeById(ids);

        return true;
    }

    /**
     * 修改套餐
     */
    @Override
    public boolean putcategory(Category category) {
        if (category==null){
            return false;
        }
        boolean b = this.updateById(category);
        return b;
    }

    @Override
    public List<Category> selctList(Integer type) {
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(type!=null,Category::getType,type);
        List<Category> list = this.list(queryWrapper);
        if (list==null){
            return null;
        }
        return list;
    }
}
